<!--
*  2021-03-15
*  纱线出库 /  化零出库  /  生产入库  /  余纱入库
-->
<template>
  <div id="app" class="yarn-weaving">
    <!-- 头部导航 -->
    <van-nav-bar
      :title="title"
      ref="header"
      left-text="退出登录"
      @click-left="onClickLeft"
    >
      <template slot="right">
        <!-- <div v-permissions="'61_471'" @click="onClickStart">盘点出库</div> -->
<!--        <div @click="onClickScan">坯布扫码</div>-->
        <van-popover v-model="showPopMenu" theme="dark" trigger="click" :actions="popMenus" placement="bottom-end" @select="selectPopMenuFn">
          <template #reference>
            <van-icon size="1.5rem" name="add-o" />
          </template>
        </van-popover>
      </template>
    </van-nav-bar>
    <better-scroll
      class="public-scroll"
      ref="wrapper"
      :data="tableData"
      :totalPage="totalPage"
    >
      <van-cell-group>
        <div
          v-for="(data, key) in tableData"
          :key="key"
          :title="data.groupLabel"
          class="yarn-weaving-wrapper"
          v-permissions="data.roles"
        >
          <div class="yarn-weaving-label">{{ data.groupLabel }}</div>
          <template v-for="(item, index) in data.value">
            <div
              v-if="item.roles"
              v-permissions="item.roles"
              :key="index"
              @click="onClickJump(item.path, item.label)"
              class="yarn-weaving-calue"
            >
              <img :src="item.src" alt="" />
              <span>{{ item.label }}</span>
            </div>
            <div
              v-else
              :key="index"
              class="yarn-weaving-calue"
              @click="onClickJump(item.path, item.label)"
            >
              <img :src="item.src" alt="" />
              <span>{{ item.label }}</span>
            </div>
          </template>
        </div>
      </van-cell-group>
    </better-scroll>
  </div>
</template>

<script>
import { openWin, closeApp, getFloat } from "@/utils/index";
import {removeInfo, setYarnList, getYarnList, getInfo, setInfo} from "@/utils/auth";
import { getYarnStoreByStoreId, getYarnPlanInfo } from "@/api/yarn";
import { getGreyClothPub } from "@/api/cloth";
import {refreshToken} from "@/api/login";

export default {
  name: "yarnWeaving",
  data() {
    return {
      title: "艺多染整",
      roles: "",
      show: false,
      btnVal: "",
      btnShow: false,
      tableData: [
        /*{
          groupLabel: "纱线仓库-出库",
          roles: "'61_62'",
          value: [{
            label: "纱线出库",
            src: require("./../../../assets/img/icon1.png"),
            path: "outbound-list",
          }, {
            label: "合纱出库",
            src: require("./../../../assets/img/icon2.png"),
            path: "outbound-yarn",
          }, {
            label: "化零出库",
            src: require("./../../../assets/img/icon3.png"),
            path: "outbound-zeroize",
          }, {
            label: "纱线跟踪",
            src: require("./../../../assets/img/icon4.png"),
            path: "list-track",
          }, {
            label: "纱线库存出库",
            src: require("./../../../assets/img/icon10.png"),
            path: "weaving-package",
          }, {
            label: "纱线库存合纱出库",
            src: require("./../../../assets/img/icon11.png"),
            path: "weaving-merge",
          }],
        },*/
        {
          groupLabel: "纱线仓库",
          roles: "'99999'",
          value: [{
            label: "纱线盘库",
            src: require("./../../../assets/img/icon9.png"),
            path: "yarn-check",
            roles: "'61_471'",
          }, {
            label: "纱线入库",
            src: require("./../../../assets/img/icon5.png"),
            path: "yarn-in",
            roles: "'61_313'",
          }, {
            label: "纱线出库",
            src: require("./../../../assets/img/icon6.png"),
            path: "yarn-out",
            roles: "'61_62'",
          }, {
            label: "细纱跟踪",
            src: require("./../../../assets/img/icon15.png"),
            path: "yarn-store",
            roles: "'486_3245'",
          }, {
            label: "外加工出库",
            src: require("./../../../assets/img/icon1.png"),
            path: "external-out",
            roles: "'61_62'",
          }],
        },
        /*{
          groupLabel: "纱线盘点",
          roles: "'61_471'",
          value: [
            {
              label: "新增编号",
              src: require("./../../../assets/img/icon7.png"),
              path: "weaving-no",
              role: "'61_471'",
            },
            {
              label: "新增色号",
              src: require("./../../../assets/img/icon8.png"),
              path: "weaving-color",
              role: "'61_471'",
            },
            {
              label: "纱线盘点入库",
              src: require("./../../../assets/img/icon9.png"),
              path: "weaving-warehouse",
              role: "'61_471'",
            },
            {
              label: "纱线盘点记录打印",
              src: require("./../../../assets/img/icon12.png"),
              path: "weaving-print",
              role: "'61_471'",
            },
          ],
        },*/
        {
          groupLabel: "织造管理",
          roles: "'57_532'",
          value: [{
            label: "坯布码单管理",
            src: require("./../../../assets/img/icon13.png"),
            path: "material-detailed",
          }, {
            label: "刻盘布头样品存放",
            src: require("./../../../assets/img/icon16.png"),
            path: "grey-sample-pos",
          }, {
            label: "机台发纱",
            src: require("./../../../assets/img/icon1.png"),
            path: "distribute-yarn",
          }]},
        {
          groupLabel: "坯布管理",
          roles: "'630_631'",
          value: [
            {
              label: "坯布盘库",
              src: require("./../../../assets/img/icon16.png"),
              path: "cloth",
            },
            {
              label: "坯布出库",
              src: require("./../../../assets/img/icon17.png"),
              path: "cloth-store",
            }, /*{
              label: "坯布逐匹扫码出库",
              src: require("./../../../assets/img/icon17.png"),
              path: "store-out-single",
            }, */{
              label: "坯布入库",
              src: require("./../../../assets/img/icon18.png"),
              path: "store-in",
            },
            {
              label: "坯布移区",
              src: require("./../../../assets/img/icon19.png"),
              path: "store-area",
            },
            {
              label: "坯布汇总",
              src: require("./../../../assets/img/icon4.png"),
              path: "cloth-sum",
            }, {
              label: "刻盘布头样品存放",
              src: require("./../../../assets/img/icon16.png"),
              path: "grey-sample-pos",
            }
          ],
        },
        {
          groupLabel: "成品管理",
          roles: "'1415_1417'",
          value: [
            {
              label: "成品跟踪",
              src: require("./../../../assets/img/icon14.png"),
              path: "product-view",
            },
            {
              label: "成品信息",
              src: require("./../../../assets/img/icon14.png"),
              path: "product-html",
            },
            {
              label: "成品盘库",
              src: require("./../../../assets/img/icon14.png"),
              path: "product-store",
            },
            {
              label: "成品移区",
              src: require("./../../../assets/img/icon14.png"),
              path: "product-change-area",
            },
            // {
            //   label: "成品详情明细",
            //   src: require("./../../../assets/img/icon14.png"),
            //   path: "product-detail",
            // },
            {
              label: "成品码单管理",
              src: require("./../../../assets/img/icon14.png"),
              path: "product",
            },
            {
              label: "码单接收",
              src: require("./../../../assets/img/icon15.png"),
              path: "product-receive",
              roles: "100103",
            }, {
              label: "成品冻结",
              src: require("./../../../assets/img/icon3.png"),
              path: "product-freeze",
            }, {
              label: "成品剪样",
              src: require("./../../../assets/img/icon2.png"),
              path: "product-sample-cut",
            }, {
              label: "退货入库",
              src: require("./../../../assets/img/icon17.png"),
              path: "product-customer-return",
            }, {
              label: "成品预检",
              src: require("./../../../assets/img/icon12.png"),
              path: "product-pre-check",
            }, {
              label: "成品样布",
              src: require("./../../../assets/img/icon12.png"),
              path: "product-sample",
            }
          ],
        },
        {
          groupLabel: "厂部数据",
          roles: "'610_611'",
          value: [
            {
              label: "成品跟踪",
              src: require("./../../../assets/img/icon14.png"),
              path: "product-view",
            },
            {
              label: "纱线跟踪",
              src: require("./../../../assets/img/icon15.png"),
              path: "list-track",
            },
            {
              label: "织造车间码单",
              src: require("./../../../assets/img/icon13.png"),
              path: "material-detailed",
            },
            {
              label: "坯布汇总",
              src: require("./../../../assets/img/icon4.png"),
              path: "cloth-sum",
            },
            {
              label: "成品信息",
              src: require("./../../../assets/img/icon14.png"),
              path: "product-html",
            },
            {
              label: "成品码单接收",
              src: require("./../../../assets/img/icon15.png"),
              path: "product-receive",
              roles: "100103",
            },
          ],
        }, {
          groupLabel: "业务管理",
          roles: "'99999'",   //通用展示
          value: [{
            label: "业务订单",
            src: require("./../../../assets/img/icon17.png"),
            path: "cloth-order",
          }],
        }, {
          groupLabel: "后整计划",
          // roles: "'3490'",
          roles: "'99999'",   //通用展示
          value: [{
            label: "坯布预出库确认",
            src: require("./../../../assets/img/icon17.png"),
            path: "cloth-store",
          }]
        }, {
          groupLabel: "投料信息",
          roles: "'99999'",
          value: [{
            label: "原料入库",
            src: require("./../../../assets/img/icon5.png"),
            path: "material-in"
          }, {
            label: "领料出库",
            src: require("./../../../assets/img/icon15.png"),
            path: "out-4-pick"
          }, {
            label: "散毛发货",
            src: require("./../../../assets/img/icon17.png"),
            path: "material-send-out"
          }]
        }, {
          groupLabel: "备件管理",
          roles: "'99999'",
          value: [{
            label: "备件盘库",
            src: require("./../../../assets/img/icon14.png"),
            path: "spare-part-check"
          }, {
            label: "采购申请",
            src: require("./../../../assets/img/icon5.png"),
            path: "spare-part-apply-for"
          }, {
            label: "备件跟踪",
            src: require("./../../../assets/img/icon9.png"),
            path: "spare-part-list"
          }]
        }, {
          groupLabel: "设备能耗安全",
          roles: "'607_5753,3944_3946'",    //
          value: [{
            label: "抄电表",
            src: require("./../../../assets/img/electricity-meter-100.png"),
            path: "electric-power"
          }, {
            label: "抄水表",
            src: require("./../../../assets/img/water-meter-100.png"),
            path: "water"
          }, {
            label: "抄蒸汽表",
            src: require("./../../../assets/img/stream-gauge-80.png"),
            path: "stream"
          }, {
            label: "消防设备巡检",
            src: require("./../../../assets/img/fire-safety-100.png"),
            path: "fire-safety"
          }, {
            label: "设备维保",
            src: require("./../../../assets/img/maintenance-100.png"),
            path: "equipment-maintenance"
          }]
        }
      ],
      totalPage: 1,

      /* 更多选项 */
      showPopMenu: false,
      popMenus: [{
        text: "坯布扫码",
        icon: "scan"
      }, {
        text: "纱线扫码",
        icon: "aim"
      }, {
        text: "成品扫码",
        icon: "label"
      }],
    };
  },
  created() {
    const that = this;
    if (process.env.NODE_ENV === "production") {
      that.$nextTick(() => {
        that.initFn(that);
      });
    } else {
      that.initFn(that);
    }
  },
  methods: {
    //初始化
    initFn(that) {
      window.apiready = function () {
        if (window.api.pageParam.posY) {
          setTimeout(() => {
            that.$refs.wrapper.scrollTo(0, window.api.pageParam.posY);
          }, 20);
        }
        closeApp();
      };

      //刷新秘钥 25分钟?
      setInterval(() => {
        refreshToken({
          secret: getInfo().secret
        }, res => {
          setInfo(res);
        });
      }, 1200000);
    },

    // 退出登录
    onClickLeft() {
      this.$dialog
        .confirm({
          title: "确认退出?",
          message: "退出后需要重新登录",
          confirmButtonText: "退出",
          confirmButtonColor: "#1677FF",
          cancelButtonColor: "#1677FF",
        })
        .then(() => {
          // 监听页面消失的时候，关掉页面就可以了
          window.api.addEventListener(
            {
              name: "viewdisappear",
            },
            function () {
              window.api.closeWin();
            }
          );
          removeInfo();
          openWin("index");
        })
        .catch(() => {
          // on cancel
        });
    },
    // 功能
    onClickRight() {
      this.show = true;
    },
    // 扫码
    onClickStart() {
      const that = this;
      let zyHmsScan = api.require('zyHmsScan');
      zyHmsScan.openDefaultView({}, ret => {
        if(ret.status) {
          let id = ret.result;
          let index = id.indexOf("taskId=");
          if (index != -1) {
            let taskId = id.slice(index + 7, id.length);
            getYarnPlanInfo(
                {
                  taskId: taskId,
                },
                (res) => {
                  let yarnList = [];
                  if (getYarnList()) {
                    yarnList = getYarnList();
                  }
                  if (res && res.length) {
                    res.forEach((item) => {
                      yarnList.push(item.yarnId);
                    });

                    let list = [...new Set(yarnList)];
                    setYarnList(list);
                    that.$toast({
                      type: "success",
                      message: "扫码成功，继续出库",
                      duration: 1500,
                      onClose: () => {
                        that.onClickStart();
                      },
                    });
                  }
                }
            );
          } else {
            if (id.indexOf(",") != -1) {
              if (!that.$isJSON(id) || id.indexOf(",") == -1) {
                that.$toast.fail("二位码异常");
                return false;
              }
              let ids = eval(id);
              if (ids.toString().indexOf(",") == -1) {
                that.$toast.fail("二位码异常");
                return false;
              }
              id = ids.split(",")[0].toString();
            }
            let yarnList = getYarnList() || [];
            if (yarnList.length === 0) {
              that.$toast.fail("请先扫生产码");
            } else {
              getYarnStoreByStoreId(id, (res) => {
                let index = yarnList.findIndex((item) => item === res.yarnId);
                if (index === -1) {
                  that.$toast.fail("非所需纱线,请扫相应生产码");
                  return false;
                } else {
                  openWin("weaving-package", {
                    standardId: res.yarnId,
                    lotNum: res.lotNum,
                    no: res.no,
                    colorName: res.colorName,
                    name: res.name,
                    standardName: res.standard,
                    stock: res.stock,
                  });
                }
              });
            }
          }
        }
      });
    },
    // 点击返回
    onClickBack() {
      this.show = false;
    },
    // 点击跳转
    onClickJump(html, title) {
      this.show = false;
      if (html) {
        openWin(html, {
          title: title,
          posY: this.$scroll.y,
        });
      } else {
        // 盘点出库
        this.onClickStart();
      }
    },
    // 查看坯布信息
    onClickScan() {
      let zyHmsScan = api.require('zyHmsScan');
      zyHmsScan.openDefaultView({}, ret => {
        if(ret.status) {
          let content = ret.result;
          if(ret.result.startsWith("29,") || ret.result.startsWith("28,") || ret.result.startsWith("27,")) {
            if(ret.result.startsWith("29,")) {
              let yarnId = ret.result.substring(3);
              openWin("yarn-info", {
                yarnId: yarnId
              });
            } else {
              openWin("yarn-info", {
                scanCode: ret.result
              });
            }
          } else if(ret.result.indexOf("http://") != -1 || ret.result.indexOf("flag=1") != -1 || ret.result.startsWith("15,")) {
            openWin("product-view", {
              scanCode: ret.result
            });
          } else {
            getGreyClothPub({
              greyClothId: content,
            }, res => {
              let miWeight = res.miWeight;
              let miWeightDesign = res.designMiWeight || miWeight;
              let className = getFloat(miWeight - miWeightDesign, 0) > 20 ? "red" : getFloat(miWeightDesign - miWeight, 0) > 20 ? "green" : "default";
              let params = {
                greyClothIds: res.id,
                storeId: res.storeId,
                designNo: res.designNo,
                colorNo: res.colorNo,
                lotNo: res.lotNo,
                designSeqNo: res.designSeqNo,
                weight: res.weight,
                length: res.length,
                miWeight: res.miWeight,
                miWeightDesign: miWeightDesign,
                className: className,
                width: res.width,
                loomNo: res.loomNo,
                jtNum: "",
                czOperator: "",
                djOperator: "",
                zbOperator: [],
                ybOperator: "",
                xbOperator: "",
                xbTime: "",
                zjOperator: "",
                receiveAt: res.receiveAt || "",
                endFlag: res.endFlag,
              };
              if (res.flowDatas && res.flowDatas.length) {
                res.flowDatas.forEach((item) => {
                  if (item.stepNo === 4 && item.stepName == "修布报工") {
                    params.xbOperator = item.operatorName;
                    params.xbTime = item.updateAt;
                  } else if (item.stepNo === 3 && item.stepName == "验布报工") {
                    params.ybOperator = item.operatorName;
                    JSON.parse(item.items[0].value).forEach((child) => {
                      params.zbOperator.push(child.no);
                    });
                  } else if (item.stepNo === 1 && item.stepName == "整经报工") {
                    params.zjOperator = item.operatorName;
                    item.items.forEach((child) => {
                      if (child.label == "机台号") {
                        params.jtNum = child.value;
                      }
                    });
                  } else if (item.stepNo === 5 && item.stepName == "穿棕报工") {
                    params.czOperator = item.operatorName;
                  } else if (item.stepNo === 10 && item.stepName == "打结报工") {
                    params.djOperator = item.operatorName;
                  }
                });
              }
              openWin("cloth-info", params);
            });
          }
        }
      });
    },

    /* 选择下拉菜单 */
    selectPopMenuFn(action, index) {
      const that = this;
      that.onClickScan();
    }
  },
};
</script>

<style lang="scss">
.yarn-weaving {
  display: flex;
  flex-direction: column;
  // padding-top: 22px;
  .van-cell-group {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    background: rgba(245, 245, 245, 1) !important;
    .yarn-weaving-wrapper {
      width: 351px;
      .yarn-weaving-label {
        padding: 30px 0 15px 0;
      }
      .yarn-weaving-calue {
        height: 80px;
        display: flex;
        align-items: center;
        background: #ffffff;
        box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.05);
        border-radius: 2px;
        margin-bottom: 20px;
        padding-left: 40px;
        img {
          width: 48px;
          height: 48px;
        }
        span {
          font-size: 18px;
          color: #333333;
          margin-left: 20px;
        }
      }
    }
  }
  .van-hairline--top-bottom::after,
  .van-hairline-unset--top-bottom::after {
    border-width: 0;
  }
}
</style>
